package com.tang.framework.config.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

/**
 * @Author tang jun liang
 * @Date 2023/8/4 14:30
 */
@JacksonStdImpl
public class LongSerializer extends StdSerializer<Object> {
    private static final long JS_NUM_MAX = 9007199254740992L;
    private static final long JS_NUM_MIN = -9007199254740992L;
    public static final LongSerializer instance = new LongSerializer();

    protected LongSerializer() {
        super(Object.class);
    }


    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        if (value instanceof Long) {
            long val = (Long) value;
            if (val <= JS_NUM_MAX && val >= JS_NUM_MIN) {
                gen.writeNumber(val);
            } else {
                gen.writeString(value.toString());
            }
        } else {
            gen.writeString(value.toString());
        }

    }
}
